package com.bojin.studio.ubattery;

import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AdviceInfoAdapter extends BaseAdapter 
{
	private List<adviceInfo> adviceList;
	private LayoutInflater mInflater;
	private Context c = null;
	
	public AdviceInfoAdapter(Context context, List<adviceInfo> results) {
		this.c = context;
		adviceList = results;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return adviceList.size();
	  }

	@Override
	public Object getItem(int position) {
		return adviceList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	  }

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		final adviceInfo adviceInfoItem = adviceList.get(position);
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.report_item, null);
	
			holder = new ViewHolder();
			holder.adviceIcon = (ImageView) convertView.findViewById(R.id.app_icon);
			holder.adviceContent = (TextView) convertView.findViewById(R.id.report_id);
	
			convertView.setTag(holder);
			convertView.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View arg0)
				{
					showDialog(c,adviceInfoItem);
				}
			});
			
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		if (adviceList == null || position < 0 || position >= adviceList.size())
			return convertView;
		
		//adviceInfo adviceInfoItem = adviceList.get(position);
		
		if (adviceInfoItem == null)
			return convertView;
		
		holder.adviceIcon.setImageDrawable(adviceInfoItem.icon);
		holder.adviceContent.setText(adviceInfoItem.content);			
		
		return convertView;
	 }
	
	public void showDialog(Context context,adviceInfo adInfo )
	{
		 Builder builder = new AlertDialog.Builder(context);	
	     builder.setTitle(adInfo.title);
	     builder.setIcon(adInfo.icon);
		 builder.setMessage(adInfo.reason+adInfo.action);
	     builder.setPositiveButton("Goto Uninstall", new DialogInterface.OnClickListener() {
	     @Override
		 public void onClick(DialogInterface dialog, int which) 
	     {
	
		            
		 }
		 });
		  
		 builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
	     @Override
	     public void onClick(DialogInterface dialog, int which)
	     {
		                
		 }
		 });
		 builder.show();		        			        
	}
	  	  
	class ViewHolder {
		ImageView adviceIcon;
		TextView adviceContent;
	}
}
